Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import { useEffect, useState } from 'react';
import i18n, { loadNamespace as loadNs } from '@/lib/i18n';
/**
* useLoadNamespace(ns)
* Dynamically loads a translation namespace and reloads it on language changes.
* Returns a boolean indicating loading state (nsLoading).
*
* Notes:
* - Uses resolvedLanguage when available.
* - Treats the namespace as "ready" only when both EN and the current language
* bundles exist, so fallbackLng works without showing raw keys.
*/
export default function useLoadNamespace(ns: string): boolean {
const initialLang = i18n.resolvedLanguage || i18n.language || 'en';
const initialLoaded =
i18n.hasResourceBundle('en', ns) && i18n.hasResourceBundle(initialLang, ns);
const [nsLoading, setNsLoading] = useState<boolean>(!initialLoaded);
useEffect(() => {
let mounted = true;
const doLoad = async (lng: string) => {
try {
setNsLoading(true);
await loadNs(lng, ns);
} finally {
if (mounted) setNsLoading(false);
}
};
// Initial load for current language.
doLoad(i18n.resolvedLanguage || i18n.language || 'en');
// Re-load on language change.
const onLanguageChanged = (lng: string) => {
doLoad(lng);
};
i18n.on('languageChanged', onLanguageChanged);
return () => {
mounted = false;
i18n.off('languageChanged', onLanguageChanged);
};
}, [ns]);
return nsLoading;
}
// Named export for compatibility
export { useLoadNamespace };
|